home *** CD-ROM | disk | FTP | other *** search
/ MacFormat 1996 March / macformat-035.iso / Shareware City / Developers / FFTs for RISC 1.0 / fftTest.c < prev    next >
Encoding:
C/C++ Source or Header  |  1995-12-19  |  3.0 KB  |  120 lines  |  [TEXT/CWIE]

  1. /*  A program to test and time complex forward and inverse fast fourier transform routines    */
  2.  
  3. #include <stdio.h>
  4. #include <stdlib.h>
  5. #include <math.h>
  6. #include <timer.h>
  7. #include "fftsbig.h"
  8.  
  9. #define    NUMROWS 1            /* process Matrix of NumRows different ffts of length N    */
  10. #define N 1024                /* size of FFT must be a power of 2 */
  11. #define NTIMES 20            /* number of timings,invalid if too big (if a[0][0].Re = 0|nan)*/
  12.  
  13. typedef  struct{
  14.     float Re;
  15.     float Im;
  16.     } Complex;
  17.  
  18. void main(){
  19. float        *Utbl;
  20. Complex        (*a)[N];
  21. UnsignedWide         TheTime1;
  22. UnsignedWide         TheTime2;
  23. UnsignedWide         TheTime3;
  24. double        TheTime;
  25. long         i, il;
  26. long         TheErr;
  27. long        M;
  28.  
  29. Utbl = (float *) calloc((N/4+1),sizeof(float));
  30. if (Utbl==0)
  31.     TheErr = 2;
  32. else
  33. TheErr = FFTInit(&M, N, Utbl);
  34.  
  35. if(!TheErr){
  36.     a = (Complex (*)[N]) calloc(NUMROWS*N,sizeof(Complex));
  37.     if (a == 0) TheErr = 2;
  38. }
  39.  
  40. if(!TheErr){
  41.             /*  set up a simple test case */
  42.     for (il=0; il<NUMROWS; il++){
  43.         for (i=0; i<N; i++){
  44.             a[il][i].Re = sqrt(il+i+.77777);    
  45.             a[il][i].Im = (il+i+.22222)*(il+i+.22222) / N - N/2;    
  46.         }
  47.         a[il][0].Re = N+3;
  48.         a[il][1].Re = 1-N;
  49.     }
  50.  
  51.     Microseconds(&TheTime1);
  52.     for (i=0;i<NTIMES;i++){        /* do NTIMES times for timing */
  53.         ffts((float *)a, M, NUMROWS, Utbl);
  54.     }    
  55.     Microseconds(&TheTime2);
  56.     for (i=0;i<NTIMES;i++){        /* do NTIMES times for timing */
  57.         iffts((float *)a, M, NUMROWS, Utbl);
  58.     }    
  59.     Microseconds(&TheTime3);
  60.  
  61.     TheTime = (double)(TheTime2.hi - TheTime1.hi) * 65536.0 * 65536.0;
  62.     TheTime = (TheTime + (double)(TheTime2.lo - TheTime1.lo))/1000.0;
  63.     printf("Time fft = %12f  ms.", TheTime/NTIMES/NUMROWS, a[0][0].Re);
  64.     TheTime = (double)(TheTime3.hi - TheTime2.hi) * 65536.0 * 65536.0;
  65.     TheTime = (TheTime + (double)(TheTime3.lo - TheTime2.lo))/1000.0;
  66.     printf("  ifft = %12f  ms. a[0][0].Re= %6e\n", TheTime/NTIMES/NUMROWS, a[0][0].Re);
  67.     printf("\n");
  68.  
  69.             /*  set up a simple test case */
  70.     for (il=0; il<NUMROWS; il++){
  71.         for (i=0; i<N; i++){
  72.             a[il][i].Re = sqrt(il+i+.77777);    
  73.             a[il][i].Im = (il+i+.22222)*(il+i+.22222) / N - N/2;    
  74.         }
  75.         a[il][0].Re = N+3;
  76.         a[il][1].Re = 1-N;
  77.     }
  78.  
  79.     ffts((float *)a, M, NUMROWS, Utbl);
  80.  
  81.     if (N*NUMROWS <= 256){
  82.          for (il=0; il<NUMROWS; il++){
  83.             printf("atrans = [ \n");
  84.             for (i=0; i<N; i++)
  85.                     printf(" %+20.15e + j * ( %+20.15e ) \n", a[il][i].Re, a[il][i].Im);
  86.             printf("]; \n");    
  87.             }
  88.         }
  89.     else { /* abbreviate big output */
  90.         printf("the first fft's last 32 values are: \n");
  91.         for (i=N-32; i<N; i++)
  92.                     printf(" %+20.15e + j * ( %+20.15e ) \n", a[0][i].Re, a[0][i].Im);
  93.     }
  94.  
  95.     iffts((float *)a, M, NUMROWS, Utbl);
  96.  
  97.     if (N*NUMROWS <= 256){
  98.          for (il=0; il<NUMROWS; il++){
  99.             printf("\n aitrans = [ \n");
  100.             for (i=0; i<N; i++)
  101.                     printf(" %+20.15e + j * ( %+20.15e ) \n", a[il][i].Re, a[il][i].Im);
  102.             printf("]; \n");    
  103.             }
  104.         }
  105.     else { /* abbreviate big output */
  106.         printf("\n the first ifft's last 32 values are: \n");
  107.         for (i=N-32; i<N; i++)
  108.                     printf(" %+20.15e + j * ( %+20.15e ) \n", a[0][i].Re, a[0][i].Im);
  109.     }
  110.  
  111.     free (a);
  112.     free (Utbl);
  113.     return;
  114. }
  115. else
  116. if(TheErr==2)    printf(" out of memory ");
  117. else    printf(" error ");
  118. return;
  119. }
  120.